﻿
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Transactions;

namespace DAL
{
    public class BackupProvider
    {
        public static bool BackupDatabase(string fileName)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    string dbname = context.Database.Connection.Database;
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        context.Database.UseTransaction(null);
                        string sqlCommand = string.Format(@"BACKUP DATABASE QLTTTH TO DISK = '{0}' WITH FORMAT, 
                                          MEDIANAME='QLTTTH', MEDIADESCRIPTION=N'Quản lý trung tâm tin học sao lưu';",
                                          fileName
                                          );
                        context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool RestoreDataBase(string fileName)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    string dbname = context.Database.Connection.Database;
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        context.Database.UseTransaction(null);
                        string sqlCommand = @"USE master RESTORE DATABASE QLTTTH FROM DISK='" + fileName + "' WITH REPLACE";
                        context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}
